package com.dragon.www.smarthome.fragment;

import android.os.Bundle;
import android.support.percent.PercentRelativeLayout;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.dragon.www.smarthome.R;
import com.dragon.www.smarthome.entity.Device;
import com.dragon.www.smarthome.entity.Room;
import com.dragon.www.smarthome.popupwindow.AirconditionPopWindow;
import com.dragon.www.smarthome.popupwindow.AllPopWindow;
import com.dragon.www.smarthome.popupwindow.BasePopupWindow;
import com.dragon.www.smarthome.popupwindow.ConferenceSetPopWindowEx;
import com.dragon.www.smarthome.popupwindow.CurtainPopWindow;
import com.dragon.www.smarthome.popupwindow.EntranceGuardPopWindow;
import com.dragon.www.smarthome.popupwindow.HumidifierPopWindow;
import com.dragon.www.smarthome.popupwindow.LampPopWindow;
import com.dragon.www.smarthome.popupwindow.PlantWallPopWindow;
import com.dragon.www.smarthome.popupwindow.RoomPopWindow;
import com.dragon.www.smarthome.util.Config;
import com.dragon.www.smarthome.util.Utils;
import com.dragon.www.smarthome.view.MainItemView;

import java.util.List;

/**
 * Created by daiye on 2016/6/1.
 */
public class FirstScreenFragment extends Fragment implements View.OnClickListener{
    private PercentRelativeLayout firstscreenMainView;
    private View mLineView;
    private MainItemView mainViewRoom;
    private MainItemView mainViewSwitch;
    private MainItemView mainViewConference;
    private MainItemView mainViewLamp;
    private MainItemView mainViewHumidifier;
    private MainItemView mainViewEntranceGuard;
    private MainItemView mainViewCurtain;
    private MainItemView mainViewAircondition;
    private MainItemView mainViewPlantWall;

    private int popWindowHeight;
    private int popWindowWidth;
    private final static float xToScreenPercent = 0.9277f;
    private final static float yToScreenPercent = 0.6551f;
    private int xOffsetToView;
    private int yOffsetToView;
    private final static float xToView = 0.0361f;
    private final static float yToView = 0.0f;
    private int roomPopWindowHeight;
    public RoomPopWindow roomPopWindow;
    private BasePopupWindow myPopWindow;
    public ConferenceSetPopWindowEx conferenceSetPopWindow;
    public AllPopWindow AllPopWindow;
    private int width;
    private int height;

    private List<Room> mRooms = null;
    private List<Device> mDevices = null;
    private Room mSelectRoom = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View entryView = inflater.inflate(R.layout.fragment_firstscreen, null);
        initView(entryView);
        return entryView;
    }

    private void initView(View entryView){
        firstscreenMainView = (PercentRelativeLayout) entryView.findViewById(R.id.firstscreen_mainView);
        mLineView = entryView.findViewById(R.id.line);
        mainViewRoom = (MainItemView) entryView.findViewById(R.id.mainView_room);
        mainViewSwitch = (MainItemView) entryView.findViewById(R.id.mainView_switch);
        mainViewConference = (MainItemView) entryView.findViewById(R.id.mainView_conference);
        mainViewLamp = (MainItemView) entryView.findViewById(R.id.mainView_lamp);
        mainViewHumidifier = (MainItemView) entryView.findViewById(R.id.mainView_humidifier);
        mainViewEntranceGuard = (MainItemView) entryView.findViewById(R.id.mainView_entrance_guard);
        mainViewCurtain = (MainItemView) entryView.findViewById(R.id.mainView_curtain);
        mainViewAircondition = (MainItemView) entryView.findViewById(R.id.mainView_aircondition);
        mainViewPlantWall = (MainItemView) entryView.findViewById(R.id.mainView_plant_wall);
        mainViewRoom.setOnClickListener(this);
        mainViewSwitch.setOnClickListener(this);
        mainViewConference.setOnClickListener(this);
        mainViewLamp.setOnClickListener(this);
        mainViewHumidifier.setOnClickListener(this);
        mainViewEntranceGuard.setOnClickListener(this);
        mainViewCurtain.setOnClickListener(this);
        mainViewAircondition.setOnClickListener(this);
        mainViewPlantWall.setOnClickListener(this);

    }

    public void initData() {
        WindowManager wm = getActivity().getWindowManager();
        width = wm.getDefaultDisplay().getWidth();
        height = wm.getDefaultDisplay().getHeight();
        popWindowHeight = (int) (height * yToScreenPercent);
        popWindowWidth = (int) (width * xToScreenPercent);
        xOffsetToView = (int) (width * xToView);
        yOffsetToView = (int) (height * yToView);
        roomPopWindowHeight = (int) (height * 0.781);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mainView_room:
                if( !Utils.isUserLogin() ){
                    Toast.makeText(getContext(), "您还没有登录，请您先登录！", Toast.LENGTH_SHORT).show();
                    return;
                }
                roomPopWindow = new RoomPopWindow(this, mRooms, popWindowWidth, roomPopWindowHeight, mLineView, xOffsetToView, yOffsetToView);
                roomPopWindow.showPopWindow();
                //隐藏主布局
                hideMainView();
                myPopWindow = null;
                break;
            case R.id.mainView_switch:
                if( Utils.isUserLogin() ) {
                    AllPopWindow = new AllPopWindow(getContext());
                    AllPopWindow.setWidth(popWindowWidth);
                    AllPopWindow.setHeight(roomPopWindowHeight);
                    AllPopWindow.setRooms(mRooms);
                    AllPopWindow.showPopWindow(mLineView, xOffsetToView, (int) (yOffsetToView + 0.5f));
                    AllPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                        @Override
                        public void onDismiss() {
                            //弹出窗消失时显示主布局
                            showMainView();
                        }
                    });
                    hideMainView();
                    myPopWindow = null;
                }
                break;
            case R.id.mainView_conference:
                if( Utils.isUserLogin() ){
                    conferenceSetPopWindow = new ConferenceSetPopWindowEx(getContext());
                    conferenceSetPopWindow.setWidth(popWindowWidth);
                    conferenceSetPopWindow.setHeight(roomPopWindowHeight);
                    conferenceSetPopWindow.setRooms(mRooms);
                    conferenceSetPopWindow.showPopWindow(mLineView, xOffsetToView, (int) (yOffsetToView + 0.5f));
                    conferenceSetPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                        @Override
                        public void onDismiss() {
                            //弹出窗消失时显示主布局
                            showMainView();
                        }
                    });
                    //隐藏主布局
                    hideMainView();
                    myPopWindow = null;
                }else {
                    Toast.makeText(getContext(), "您还没有登录", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.mainView_lamp:
                myPopWindow = new LampPopWindow(getContext());
                break;
            case R.id.mainView_humidifier:
                myPopWindow = new HumidifierPopWindow(getContext());
                break;
            case R.id.mainView_entrance_guard:
                myPopWindow = new EntranceGuardPopWindow(getContext());
                break;
            case R.id.mainView_plant_wall:
                myPopWindow = new PlantWallPopWindow(getContext());
                break;
            case R.id.mainView_curtain:
                myPopWindow = new CurtainPopWindow(getContext());
                break;
            case R.id.mainView_aircondition:
                myPopWindow = new AirconditionPopWindow(getContext());
                break;
        }
        if (myPopWindow != null && mSelectRoom != null) {
            myPopWindow.setRoomId(mSelectRoom.getRoomid());
            myPopWindow.setDevices(mDevices);
            myPopWindow.setWidth(popWindowWidth);
            myPopWindow.setHeight(roomPopWindowHeight);
            myPopWindow.showPopWindow(mLineView, xOffsetToView, (int) (yOffsetToView  + 0.5f));
            myPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    //弹出窗消失时显示主布局
                    showMainView();
                }
            });
            //隐藏主布局
            hideMainView();
        }else if(myPopWindow != null){
            Toast.makeText(getContext(), "您还没有登录或选择任何房间！", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 从网络获取房间后设置对应的app设备，包括自动恢复上次保存的room信息
     * 2016/06/01
     */
    public void setRooms(List<Room> rooms){
        this.mRooms = rooms;
        // 如果保存了房间信息从本地读取房间信息,自动选中
        if (Config.getRoomId() != -1) {
            Room room = getRoom();
            for(int i = 0; i<mRooms.size(); i++){
                if( mRooms.get(i).getRoomid() == room.getRoomid())
                {
                    selectRoom(mRooms.get(i));
                    break;
                }
            }
        }
    }

    public void setDevices(List<Device> devices) {
        this.mDevices = devices;
    }

    /**
     * 在房间界面选中某一个房间后，切换到该房间，包括保存到本地
     * 2016/06/01
     */
    public void selectRoom(Room room){
        if (room == null){
            Log.e("房间为空","房间为空1111111");
        }
        if (mainViewRoom == null){
            Log.e("房间为空","房间为空2222222");
        }
        this.mSelectRoom = room;
        if(mainViewRoom!=null)
        mainViewRoom.setRoomInfo(this.mSelectRoom);
        saveRoom(this.mSelectRoom);
        if(mListener!=null)
            mListener.onSelectRoomChanged(this.mSelectRoom);
    }

    /**
     * 从本地读出保存的房间
     * 2016/06/01
     */
    private Room getRoom(){
        Room room = new Room();
        room.setRoomid(Config.getRoomId());
        room.setRoomName(Config.getRoomName());
        room.setRoomDesciption(Config.getRoomDesc());
        room.setImgPath(Config.getRoomBg());
        return room;
    }

    /**
     * 向本地写入选中的房间
     * 2016/06/01
     */
    private void saveRoom(Room room){
        Config.setRoomId(room.getRoomid());
        Config.setRoomName(room.getRoomName());
        Config.setRoomDesc(room.getRoomDesciption());
        Config.setRoomBg(room.getImgPath());
    }

    /**
     * 显示设备界面的时候隐藏主方块界面
     * 2016/06/01
     */
    public void hideMainView() {
        firstscreenMainView.setVisibility(View.GONE);
        if(mListener!=null){
            mListener.onHideOperateBar();
        }
    }

    /**
     * 设备对话框关闭的时候显示主方块界面
     * 2016/06/01
     */
    public void showMainView() {
        firstscreenMainView.setVisibility(View.VISIBLE);
        if(mListener!=null){
            mListener.onShowOperateBar();
        }
    }

    /**
     * 将该FirstScreenFragment的一些操作映射到它的父容器，操作父容器的界面
     * 2016/06/01
     */
    private OnOperateListener mListener = null;

    public void setOnOperateListener(OnOperateListener l) {
        mListener = l;
    }

    public interface OnOperateListener {
        void onHideOperateBar();
        void onShowOperateBar();
        void onSelectRoomChanged(Room changedRoom);
    }
}
